home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / GraphicsTest.java < prev    next >
Text File  |  1998-09-15  |  12KB  |  586 lines

  1. /*
  2.  * @(#)GraphicsTest.java    1.4 96/12/06
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. import java.awt.*;
  32. import java.util.*;
  33. import java.awt.event.*;
  34. import java.applet.Applet;
  35.  
  36. class GraphicsPanel extends Panel {
  37.     ActionListener al;
  38.     ItemListener il;
  39.     public GraphicsCards cards;
  40.   
  41.      GraphicsPanel(EventListener listener) {
  42.          al = (ActionListener)listener;
  43.          il = (ItemListener)listener;
  44.     
  45.         setLayout(new BorderLayout());
  46.     
  47.         add("Center", cards = new GraphicsCards());
  48.     
  49.         Panel p = new Panel();
  50.         //p.setLayout(new BorderLayout());
  51.     
  52.         Button b = new Button("next");
  53.         b.addActionListener(al);
  54.         p.add(b);
  55.     
  56.         b = new Button("previous");
  57.         b.addActionListener(al);
  58.         p.add(b);
  59.     
  60.         p.add(new Label("go to:", Label.RIGHT));
  61.     
  62.         Choice c = new Choice();
  63.         c.addItemListener(il);
  64.         p.add(c);
  65.     
  66.         c.addItem("Arc");
  67.         c.addItem("Oval");
  68.         c.addItem("Polygon");
  69.         c.addItem("Rect");
  70.         c.addItem("RoundRect");
  71.     
  72.         add("North", p);
  73.     
  74.         setSize(400, 400);
  75.     }
  76.   
  77.     public Dimension getPreferredSize() {
  78.         return new Dimension(200, 100);
  79.     }
  80. }
  81.  
  82. public class GraphicsTest extends Applet 
  83. implements ActionListener, ItemListener {
  84.     GraphicsPanel mainPanel;
  85.  
  86.     public void init() {
  87.         setLayout(new BorderLayout());
  88.         add("Center", mainPanel = new GraphicsPanel(this));
  89.     }
  90.  
  91.     public void destroy() {
  92.         remove(mainPanel);
  93.     }
  94.  
  95.     public void actionPerformed(ActionEvent e) {
  96.         String arg = e.getActionCommand();
  97.  
  98.         if ("next".equals(arg)) {
  99.             ((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards);
  100.         }
  101.         else if ("previous".equals(arg)) {
  102.             ((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards);
  103.         }
  104.     }
  105.   
  106.     public void itemStateChanged(ItemEvent e) {
  107.         ((CardLayout)mainPanel.cards.getLayout()).show(mainPanel.cards,(String)e.getItem());
  108.     }
  109.   
  110.     public static void main(String args[]) {
  111.         AppletFrame.startApplet("GraphicsTest", "Graphics Test", args);
  112.     }
  113.   
  114.     public String getAppletInfo() {
  115.         return "An interactive demonstration of some graphics.";
  116.     }
  117. }   // end class GraphicsTest
  118.  
  119.  
  120. class GraphicsCards extends Panel {
  121.     public GraphicsCards() {
  122.         setLayout(new CardLayout());
  123.         add("Arc", new ArcCard());
  124.         add("Oval", new ShapeTest( new OvalShape() ) );
  125.         add("Polygon", new ShapeTest( new PolygonShape() ) );
  126.         add("Rect", new ShapeTest( new RectShape() ) );
  127.         add("RoundRect", new ShapeTest( new RoundRectShape() ) );
  128.     }
  129. }   // end class GraphicsCards
  130.  
  131.  
  132. class ArcCard extends Panel {
  133.     public ArcCard() {
  134.         setLayout(new GridLayout(0, 2));
  135.         add(new ArcPanel(true));
  136.         add(new ArcPanel(false));
  137.         add(new ArcDegreePanel(true));
  138.         add(new ArcDegreePanel(false));
  139.     }
  140. }   // end class ArcCard
  141.  
  142.  
  143. class ArcDegreePanel extends Panel {
  144.     boolean filled;
  145.   
  146.     public ArcDegreePanel(boolean filled) {
  147.         this.filled = filled;
  148.     }
  149.   
  150.     void arcSteps(Graphics g, 
  151.                   int step, 
  152.                   int x, 
  153.                   int y, 
  154.                   int w, 
  155.                   int h, 
  156.                   Color c1,  
  157.                   Color c2) {
  158.     int a1 = 0;
  159.     int a2 = step;
  160.     int progress = 0;
  161.     g.setColor(c1);
  162.     for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1) {
  163.     if (g.getColor() == c1) {
  164.         g.setColor(c2);
  165.         }
  166.     else {
  167.         g.setColor(c1);
  168.         }
  169.     
  170.     if (filled) {
  171.         g.fillArc(x, y, w, h, a1, a2);
  172.         }
  173.     else {
  174.         g.drawArc(x, y, w, h, a1, a2);
  175.         }
  176.     
  177.     progress = a1+a2;
  178.       }  // end for
  179.     
  180.     if (progress != 360) {
  181.           if (filled) {
  182.         g.fillArc(x, y, w, h, a1, 360 - progress);
  183.       }
  184.     else {
  185.         g.drawArc(x, y, w, h, a1, 360 - progress);
  186.         }
  187.       }  // end if
  188.   }  // end arcSteps()
  189.   
  190.     public void paint(Graphics g) {
  191.         Rectangle r = getBounds();
  192.     
  193.         arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue);
  194.     
  195.         arcSteps(g, 
  196.                  2, 
  197.                  r.width / 4, 
  198.                  r.height / 4, 
  199.                  r.width / 2, 
  200.                  r.height / 2, 
  201.                  Color.yellow, 
  202.                  Color.green);
  203.     
  204.         arcSteps(g, 
  205.                  1, 
  206.                  (r.width  * 3) / 8, 
  207.                  (r.height * 3) / 8, 
  208.                  r.width / 4, 
  209.                  r.height / 4, 
  210.                  Color.magenta, 
  211.                  Color.white);
  212.     
  213.   }  // end paint()
  214. }   // end class ArcDegreePanel
  215.  
  216.  
  217. class ArcPanel extends Panel {
  218.     boolean filled;
  219.   
  220.     public ArcPanel(boolean filled) {
  221.     this.filled = filled;
  222.   }
  223.   
  224.   public void paint(Graphics g) 
  225.   {
  226.     Rectangle r = getBounds();
  227.     
  228.     g.setColor(Color.yellow);
  229.     if (filled) 
  230.       {
  231.     g.fillArc(0, 0, r.width, r.height, 0, 45);
  232.       } 
  233.     else 
  234.       {
  235.     g.drawArc(0, 0, r.width, r.height, 0, 45);
  236.       }
  237.     
  238.     g.setColor(Color.green);
  239.     if (filled) 
  240.       {
  241.     g.fillArc(0, 0, r.width, r.height, 90, -45);
  242.       } 
  243.     else 
  244.       {
  245.     g.drawArc(0, 0, r.width, r.height, 90, -45);
  246.       }
  247.     
  248.     g.setColor(Color.orange);
  249.     if (filled) 
  250.       {
  251.     g.fillArc(0, 0, r.width, r.height, 135, -45);
  252.       } 
  253.     else 
  254.       {
  255.     g.drawArc(0, 0, r.width, r.height, 135, -45);
  256.       }
  257.     
  258.     g.setColor(Color.magenta);
  259.     
  260.     if (filled) 
  261.       {
  262.     g.fillArc(0, 0, r.width, r.height, -225, 45);
  263.       } 
  264.     else 
  265.       {
  266.     g.drawArc(0, 0, r.width, r.height, -225, 45);
  267.       }
  268.     
  269.     g.setColor(Color.yellow);
  270.     if (filled) 
  271.       {
  272.     g.fillArc(0, 0, r.width, r.height, 225, -45);
  273.       } 
  274.     else  
  275.       {
  276.     g.drawArc(0, 0, r.width, r.height, 225, -45);
  277.       }
  278.     
  279.     g.setColor(Color.green);
  280.     if (filled) 
  281.       {
  282.     g.fillArc(0, 0, r.width, r.height, -135, 45);
  283.       } 
  284.     else 
  285.       {
  286.     g.drawArc(0, 0, r.width, r.height, -135, 45);
  287.       }
  288.     
  289.     g.setColor(Color.orange);
  290.     if (filled) 
  291.       {
  292.     g.fillArc(0, 0, r.width, r.height, -45, -45);
  293.       } 
  294.     else 
  295.       {
  296.     g.drawArc(0, 0, r.width, r.height, -45, -45);
  297.       }
  298.     
  299.     g.setColor(Color.magenta);
  300.     if (filled) 
  301.       {
  302.     g.fillArc(0, 0, r.width, r.height, 315, 45);
  303.       } 
  304.     else 
  305.       {
  306.     g.drawArc(0, 0, r.width, r.height, 315, 45);
  307.       }
  308.     
  309.   }  // end paint()
  310.   
  311. }   // end class ArcPanel
  312.  
  313.  
  314. abstract class Shape 
  315. {
  316.   abstract void draw(Graphics g, int x, int y, int w, int h);
  317.   abstract void fill(Graphics g, int x, int y, int w, int h);
  318. }
  319.  
  320.  
  321. class RectShape extends Shape 
  322. {
  323.   void draw(Graphics g, int x, int y, int w, int h) 
  324.   {
  325.     g.drawRect(x, y, w, h);
  326.   }
  327.   
  328.   void fill(Graphics g, int x, int y, int w, int h) 
  329.   {
  330.     g.fillRect(x, y, w, h);
  331.   }
  332. }
  333.  
  334.  
  335. class OvalShape extends Shape 
  336. {
  337.   void draw(Graphics g, int x, int y, int w, int h) 
  338.   {
  339.     g.drawOval(x, y, w, h);
  340.   }
  341.   
  342.   void fill(Graphics g, int x, int y, int w, int h) 
  343.   {
  344.     g.fillOval(x, y, w, h);
  345.   }
  346. }
  347.  
  348.  
  349. class RoundRectShape extends Shape 
  350. {
  351.   void draw(Graphics g, int x, int y, int w, int h) 
  352.   {
  353.     g.drawRoundRect(x, y, w, h, 10, 10);
  354.   }
  355.   
  356.   void fill(Graphics g, int x, int y, int w, int h) 
  357.   {
  358.     g.fillRoundRect(x, y, w, h, 10, 10);
  359.   }
  360. }
  361.  
  362. class PolygonShape extends Shape 
  363. {
  364.   // class variables
  365.   Polygon p;
  366.   Polygon pBase;
  367.   
  368.   public PolygonShape() 
  369.   {
  370.     pBase = new Polygon();
  371.     pBase.addPoint(0, 0);
  372.     pBase.addPoint(10, 0);
  373.     pBase.addPoint(5, 15);
  374.     pBase.addPoint(10, 20);
  375.     pBase.addPoint(5, 20);
  376.     pBase.addPoint(0, 10);
  377.     pBase.addPoint(0, 0);
  378.   }
  379.   
  380.   void scalePolygon(float w, float h)
  381.   {
  382.     p = new Polygon();
  383.     for (int i = 0; i < pBase.npoints; ++i)
  384.       {
  385.     p.addPoint( (int) (pBase.xpoints[i] * w), 
  386.             (int) (pBase.ypoints[i] * h) );
  387.       }
  388.     
  389.   }
  390.   
  391.   void draw(Graphics g, int x, int y, int w, int h) 
  392.   {
  393.     Graphics ng = g.create();
  394.     ng.translate(x, y);
  395.     scalePolygon( (float) ( (float) w / (float) 10 ), 
  396.           (float) ( (float) h / (float) 20 ) );
  397.     ng.drawPolygon(p);
  398.   }
  399.   
  400.   void fill(Graphics g, int x, int y, int w, int h) 
  401.   {
  402.     Graphics ng = g.create();
  403.     ng.translate(x, y);
  404.     scalePolygon( (float) ( (float) w / (float) 10 ), 
  405.           (float) ( (float) h / (float) 20 ) );
  406.     ng.fillPolygon(p);
  407.   }
  408. }
  409.  
  410.  
  411. class ShapeTest extends Panel 
  412. {
  413.   Shape   shape;
  414.   int        step;
  415.   
  416.   public ShapeTest(Shape shape, int step) 
  417.   {
  418.     this.shape = shape;
  419.     this.step = step;
  420.   }
  421.   
  422.   public ShapeTest(Shape shape) 
  423.   {
  424.     this(shape, 10);
  425.   }
  426.   
  427.     public void paint(Graphics g) {
  428.         Rectangle bounds = getBounds();
  429.     
  430.         int cx, cy, cw, ch;
  431.     
  432.         Color color;
  433.     
  434.         for (color=Color.red, 
  435.                  cx=bounds.x, 
  436.                  cy=bounds.y, 
  437.                  cw=bounds.width / 2, 
  438.                  ch=bounds.height;
  439.              cw > 0 && ch > 0;
  440.      
  441.              cx+=step, 
  442.                  cy += step, 
  443.                  cw -= (step * 2), 
  444.                  ch -= (step * 2),
  445.                  color=ColorUtils.darker(color, 0.9) ) {
  446.             g.setColor(color);
  447.             shape.draw(g, cx, cy, cw, ch);
  448.         }
  449.     
  450.         for (cx=bounds.x + bounds.width / 2, 
  451.                  cy=bounds.y,
  452.                  cw=bounds.width / 2, ch=bounds.height; 
  453.              cw > 0 && ch > 0;
  454.       
  455.              cx+=step, 
  456.                  cy += step, 
  457.                  cw -= (step * 2),
  458.                  ch -= (step * 2) ) {
  459.             if (g.getColor() == Color.red) {
  460.                 g.setColor(Color.blue);
  461.             }
  462.             else {
  463.                 g.setColor(Color.red);
  464.             }
  465.     
  466.             shape.fill(g, cx, cy, cw, ch);
  467.         }  // end for
  468.     }  // end paint()
  469. }   // end class ShapeTest
  470.  
  471. class ColorUtils {
  472.     static Color brighter(Color c, double factor) {
  473.         return new Color( Math.min((int)(c.getRed()  *(1/factor)), 255), 
  474.                           Math.min((int)(c.getGreen()*(1/factor)), 255),
  475.                           Math.min((int)(c.getBlue() *(1/factor)), 255) );
  476.     }
  477.   
  478.     static Color darker(Color c, double factor) {
  479.         return new Color( Math.max((int)(c.getRed()  *factor), 0), 
  480.                           Math.max((int)(c.getGreen()*factor), 0),
  481.                           Math.max((int)(c.getBlue() *factor), 0) );
  482.     }
  483. }
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.